package com.senditcertified.mail;

import java.io.IOException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.Utility.Base64;
import com.Utility.Utility;
import com.activities.group.TabHomeGroupActivity;
import com.senditcertified.mail.R;
import com.parser.Error_Report;
import com.parser.Parser_Soap;
import com.soap.call.Http_Soap_Call;

public class Password extends Activity {
	/** Called when the activity is first created. */
	private EditText username, password;
	private Button login;
	ProgressDialog pd;
	ImageView imag;
	String Username = "";

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.password);
		Username = this.getIntent().getExtras().getString("Username");
		findViewByID();
		login.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if (TextUtils.isEmpty(password.getText().toString())) {
					Toast.makeText(Password.this, "plz enter your password",
							Toast.LENGTH_SHORT).show();
					return;

				}
				/*
				 * else
				 * if(!Utility.isEmailValid((username.getText().toString()))){
				 * Toast.makeText(Password.this,
				 * "plz enter valid email address", Toast.LENGTH_SHORT) .show();
				 * return; }
				 */
				new LoginOnlineTask().execute(password.getText().toString());
			}
		});

		// Http_Soap_Call call = Http_Soap_Call.create_connection("", this);
		// parse.GetLoginToken(call.GetLoginToken("zxzx@dd.com", "123456"));
		// parse.GetTime(call.GetTime());
		// call.GetUser("fff", "dfdf");
		// parse.GetUser(call.GetUser("fff", "dfdf"));
		// call.GetUserSecurityImage("dffdfdf");
	}

	private void findViewByID() {
		// username = (EditText) findViewById(R.id.username);
		password = (EditText) findViewById(R.id.password);
		login = (Button) findViewById(R.id.login_btn);
		imag = (ImageView) findViewById(R.id.imag);

	}

	class LoginOnlineTask extends AsyncTask<String, Void, Boolean> {
		boolean result = false;
		// String error_msg = "";
		String Result = null;
		Parser_Soap parse = new Parser_Soap();
		Error_Report repor = new Error_Report();

		protected Boolean doInBackground(String... params) {
			Http_Soap_Call call = Http_Soap_Call.create_connection("",
					Password.this);
			if (call != null) {
				Result = parse.GetUser(
						call.GetUser(Username, params[0], repor), repor);
				if (Result != null) {
					System.out.println("true");
					if (Result.contains("Password Error")) {
						repor.setMessage("Password Incorrect.");
						result = false;
					} else {

						result = true;
					}

				} else {
					// error_msg=Utility.INVALID_USER;
					System.out.println("false");
					result = false;
					repor.setMessage("Internal Error");
				}

			} else {
				repor.setMessage(Utility.NETWORK_ISSUE);
				return false;
			}

			return result;
		}

		protected void onPreExecute() {
			pd = ProgressDialog.show(Password.this, null,
					getString(R.string.loader));
		}

		protected void onPostExecute(Boolean result) {
			// This method runs in the UI thread

			pd.dismiss();
			if (result) {
				Utility.printLog("Login Responce", "Login successfully");
				Utility.setStringPreference(Password.this, "username", Username);
				Utility.setStringPreference(Password.this, "password", password
						.getText().toString());

				/*
				 * Toast.makeText(Password.this, Result, Toast.LENGTH_SHORT)
				 * .show();
				 */

				Intent mainIntent = new Intent(Password.this,
						TabHomeGroupActivity.class);
				Password.this.startActivity(mainIntent);
				Password.this.finish();
			} else {
				Utility.printLog("Login Responce", "Login Unsuccessfully");
				Toast.makeText(Password.this, repor.getMessage(),
						Toast.LENGTH_SHORT).show();
			}

		}
	}

}